VERSION       = File.read("../VERSION.txt").strip
RUBY_VERSIONS = File.read("../RUBY_VERSIONS.txt").strip.split(/\s+/)

task :default => :package

desc "Create packages for all Ruby versions (#{RUBY_VERSIONS.join(' ')})"
task :package do
  # Do nothing
end

desc "Build the runtime"
task :runtime => "runtime/ok"

desc "Build binaries for all Ruby versions (#{RUBY_VERSIONS.join(' ')})"
task :build do
  # Do nothing
end

desc "Test all Ruby versions (#{RUBY_VERSIONS.join(' ')})"
task :test do
  # Do nothing
end

desc "Upload all packages to the server"
task :upload do
  # Do nothing
end

desc "Clean all packages, but not the runtime"
task :clean do
  sh "rm -rf output"
end

desc "Clean everything, including the runtime"
task "clean-all" => :clean do
  sh "rm -rf runtime"
end

desc "Stash away files/directories conflicting with the build process"
task :stash_conflicting_paths do
  if File.exist?(File.expand_path("~/.bundle/config"))
    sh "sudo mv -n ~/.bundle/config ~/.bundle/config.renamed"
  end
  if File.exist?("/usr/local/include")
    sh "sudo mv -n /usr/local/include /usr/local/include.renamed"
  end
  if File.exist?("/usr/local/lib")
    sh "sudo mv -n /usr/local/lib /usr/local/lib.renamed"
  end
end

desc "Restore the stash away files/directories"
task :unstash_conflicting_paths do
  if File.exist?(File.expand_path("~/.bundle/config.renamed"))
    sh "sudo mv -n ~/.bundle/config.renamed ~/.bundle/config"
  end
  if File.exist?("/usr/local/include.renamed")
    sh "sudo mv -n /usr/local/include.renamed /usr/local/include"
  end
  if File.exist?("/usr/local/lib.renamed")
    sh "sudo mv -n /usr/local/lib.renamed /usr/local/lib"
  end
end


RUBY_VERSIONS.each do |ruby_version|
  package = "traveling-ruby-#{VERSION}-#{ruby_version}-osx.tar.gz"
  gem_dir = "traveling-ruby-gems-#{VERSION}-#{ruby_version}-osx"

  task :package => "package:#{ruby_version}"
  task :build   => "build:#{ruby_version}"
  task :test    => "test:#{ruby_version}"
  task :upload  => "upload:#{ruby_version}"
  task :clean   => "clean:#{ruby_version}"

  desc "Create packages for Ruby #{ruby_version}"
  task "package:#{ruby_version}" => [package, "#{gem_dir}/ok"]

  desc "Build binaries for Ruby #{ruby_version}"
  task "build:#{ruby_version}" => "output/#{ruby_version}/bin"


  file(package => "output/#{ruby_version}/bin") do
    sh "./package -r #{package} output/#{ruby_version}"
  end

  file("#{gem_dir}/ok" => "output/#{ruby_version}/bin") do
    sh "./package -E #{gem_dir} output/#{ruby_version}"
    touch "#{gem_dir}/ok"
  end

  # We use 'file' instead of 'directory' here so that packages are updated
  # whenever we update binaries.
  file("output/#{ruby_version}/bin" => "runtime/ok") do
    sh "mkdir -p output/#{ruby_version}"
    sh "mkdir -p /tmp/ruby-#{ruby_version}"
    sh "./build-ruby -r #{ruby_version} -w /tmp/ruby-#{ruby_version} runtime output/#{ruby_version}"
  end


  desc "Test Ruby #{ruby_version}"
  task "test:#{ruby_version}" => "output/#{ruby_version}/bin" do
    sh "./test-gems output/#{ruby_version}"
  end

  desc "Upload Ruby #{ruby_version} packages to the server"
  task "upload:#{ruby_version}" => [package, "#{gem_dir}/ok"] do
    sh "s3cmd -P sync --no-preserve #{package} s3://traveling-ruby/releases/"
    sh "s3cmd -P --delete-removed --no-preserve -r sync #{gem_dir}/ s3://traveling-ruby/releases/#{gem_dir}/"
    sh "s3cmd del s3://traveling-ruby/releases/#{gem_dir}/ok"
  end

  desc "Clean Ruby #{ruby_version} packages, but not the runtime"
  task "clean:#{ruby_version}" do
    sh "rm -rf #{package} #{gem_dir} output/#{ruby_version}"
  end
end

file "runtime/ok" => "setup-runtime" do
  sh "./setup-runtime runtime"
  sh "touch runtime/ok"
end
